import { createORPCClient } from "@orpc/client";
import { RPCLink } from "@orpc/client/fetch";
import { createTanstackQueryUtils } from "@orpc/tanstack-query";
import { QueryCache, QueryClient } from "@tanstack/react-query";
import type { AppRouterClient } from "@{{projectName}}/api/routers/index";
{{#if (eq auth "better-auth")}}
import { authClient } from "@/lib/auth-client";
{{/if}}

export const queryClient = new QueryClient({
  queryCache: new QueryCache({
    onError: (error) => {
      console.log(error)
    },
  }),
});

export const link = new RPCLink({
  {{#if (eq backend "self")}}
  url: `${process.env.EXPO_PUBLIC_SERVER_URL}/api/rpc`,
  {{else}}
  url: `${process.env.EXPO_PUBLIC_SERVER_URL}/rpc`,
  {{/if}}
  {{#if (eq auth "better-auth")}}
  headers() {
    const headers = new Map<string, string>();
    const cookies = authClient.getCookie();
    if (cookies) {
      headers.set("Cookie", cookies);
    }
    return Object.fromEntries(headers);
  },
  {{/if}}
});

export const client: AppRouterClient = createORPCClient(link);

export const orpc = createTanstackQueryUtils(client);
